<template>
  <RadioGroup v-bind="attrs" v-model:value="state" @change="handleChange">
    <template v-for="item in getOptions" :key="`${item.value}`">
      <Radio :value="item.value" :style="{ color: item?.color, ...style }" :disabled="item.disabled">
        {{ item.label }}
      </Radio>
    </template>
  </RadioGroup>
</template>
<script lang="ts">
  import { CSSProperties, defineComponent, PropType, computed, ref, unref, watch, watchEffect } from "vue"
  import { Radio } from "ant-design-vue"
  import { isEmpty, isString, isBoolean } from "/@/utils/is"
  import { propTypes } from "/@/utils/propTypes"
  import { useAttrs } from "/@/hooks/core/useAttrs"

  type OptionsItem = { label: string; value: string | number | boolean; disabled?: boolean; color: string }
  type RadioItem = string | OptionsItem

  export default defineComponent({
    name: "MyRadioGroup",
    components: { RadioGroup: Radio.Group, Radio },
    inheritAttrs: false,
    props: {
      value: {
        type: [String, Number, Boolean, Object] as PropType<string | number | boolean | object>,
      },
      options: {
        type: Array as PropType<RadioItem[]>,
        default: () => [],
      },
      style: { type: Object as PropType<CSSProperties>, default: () => ({}) },
      dictType: propTypes.string,
    },
    emits: ["change", "update:value"],
    setup(props, { emit }) {
      const attrs = useAttrs()
      const optionsRef = ref<RadioItem[]>(props.options)
      const state = ref()
      const isBoolRef = ref(false)

      if (!isEmpty(props.dictType)) {
        // const { initSelectOptions } = useDict();
        // initSelectOptions(optionsRef, props.dictType);
      }

      watch(
        () => props.options,
        () => {
          isBoolRef.value = props.options.some((item: any) => isBoolean(item?.value))
          optionsRef.value = props.options
        }
      )

      watchEffect(() => {
        state.value = unref(isBoolRef) ? (props.value ? 1 : 0) : props.value
      })

      const getOptions = computed((): OptionsItem[] => {
        const options = unref(optionsRef)
        if (!options || options?.length === 0) return []
        const isStringArr = options.some((item) => isString(item))
        isBoolRef.value = options.some((item: any) => isBoolean(item?.value))
        if (!isStringArr)
          return options.map((item: OptionsItem) => ({
            ...item,
            label: item.label,
            value: isBoolRef.value ? (item.value ? 1 : 0) : item.value,
            color: item.color,
          })) as OptionsItem[]
        return options.map((item) => ({ label: item, value: item })) as OptionsItem[]
      })

      const handleChange = (e) => {
        const value = e.target.value
        emit("change", value)
        emit("update:value", value)
      }

      return { attrs, state, getOptions, handleChange }
    },
  })
</script>
<style lang="less">
  .ant-radio-wrapper {
    margin-right: 0;
    margin-left: 4px;
  }

  .ant-radio-checked {
    .ant-radio-inner {
      opacity: 0.9;
    }
  }
</style>
